﻿#include <QSqlError>
#include "GoodsKind.h"
#include "DatabaseMgmt.h"


CGoodsKind::CGoodsKind()
{
	init();
}

CGoodsKind::~CGoodsKind()
{

}

void CGoodsKind::init()
{

}

int CGoodsKind::insertRow(GoodsKind info)
{
	int ret = 0;

	QString strSql = QString("INSERT INTO TB_GOODS_KIND (NAME) VALUES ('%1')")
		.arg(info.name);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
	}

	return ret;
}

int CGoodsKind::queryAll(QVector<GoodsKind>& kinds)
{
	int ret = 0;

	QString strSql = QString("SELECT UID, NAME FROM TB_GOODS_KIND");

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
	}
	else
	{
		while (query.next())
		{
			GoodsKind kind;

			kind.uid = query.value("UID").toUInt();
			kind.name = query.value("NAME").toString();

			kinds.append(kind);
		}
	}
	return ret;
}

int CGoodsKind::queryByID(quint32 uid, GoodsKind& info)
{
	int ret = 0;

	QString strSql = QString("SELECT UID, NAME FROM TB_GOODS_KIND WHERE UID=%1")
		.arg(uid);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.next())
	{
		info.uid = query.value("UID").toUInt();
		info.name = query.value("NAME").toString();
	}
	else
	{
		if (query.lastError().type() != QSqlError::NoError)
		{
			ret = CONST_ERR_EXEC_SQL_FAILED;
		}
	}

	return ret;
}

int CGoodsKind::removeRow(quint32 uid)
{
	int ret = 0;

	QString strSql = QString("DELETE FROM TB_GOODS_KIND WHERE UID=%1")
		.arg(uid);
	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);
	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
	}

	return ret;
}

